<div class="layui-fluid">
    <div class="layui-row">
        <div class="layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-card-header">
                        <div class="layui-row" id="title-label">
                        </div>
                    </div>

                    <div class="layui-card-body">
                        <form class="layui-form" action="" accept-charset="UTF-8" method="post" id="base_form" lay-filter="base_form">
                            <input id="at_id" type="hidden" name="id">
                            <div class="layui-row">
                                <div class="layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">单位名称</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="name" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">

                                <div class="layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">邮箱</label>

                                        <div class="layui-input-block">
                                            <input type="text" name="email" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">考点编号</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="examinationCode" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">省</label>
                                        <div class="layui-input-block">
                                            <select id="provno_id" name="provno" lay-filter="provinceSelect">
									        	<option value="" selected="">请选择</option>
									        </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">市</label>

                                        <div class="layui-input-block">
                                            <select id="cityno_id" name="cityno" lay-filter="citySelect">
									        	<option value="" selected="">请选择</option>
									        </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">区</label>

                                        <div class="layui-input-block">
                                            <select id="areacode_id" name="areacode">
										    	<option value="" selected="">请选择</option>
										    </select>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">考点负责人</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="chargeName" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">负责人职务</label>

                                        <div class="layui-input-block">
                                            <input type="text" name="chargePost" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">负责人电话</label>

                                        <div class="layui-input-block">
                                            <input type="text" name="chargePhone" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">考点固话</label>

                                        <div class="layui-input-block">
                                            <input type="text" name="mobile" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">考务联系人</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="examinationName" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">联系人职务</label>

                                        <div class="layui-input-block">
                                            <input type="text" name="examinationPost" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">联系人手机</label>

                                        <div class="layui-input-block">
                                            <input type="text" name="examinationPhone" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label">到期时间</label>
                                <div class="layui-input-block">
                                    <input type="text" name="endTime" placeholder="" autocomplete="off" class="layui-input" id="endTime_id">
                                </div>
                            </div>

                            <div class="layui-form-item">


                                <div class="layui-row">
                                    <div class="layui-col-md6">
                                        <label class="layui-form-label">考点定位</label>
                                        <div class="layui-input-block">
                                            <input id="address_input" type="text" name="companyAddress" required lay-verify="required" placeholder="" autocomplete="on" class="layui-input">
                                        </div>
                                    </div>

                                    <div class="layui-col-md6">
                                        <label class="layui-form-label">详细地址</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="companyAddressDetail" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">营业执照</label>
                                        <div class="layui-upload">
                                            <button type="button" class="layui-btn layui-btn-normal" id="test1_hash">上传图片</button>
                                            <div class="layui-upload-list">
												<!-- <img class="layui-upload-img" id="demo1_hash" style="width: 10%;margin-left: 10%;" onclick="deletePic()"/> -->
											</div>
                                        </div>
                                    </div>
                                </div>

                                <!-- <div class="layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">公司环境</label>
                                        <div class="layui-upload">
                                            <button type="button" class="layui-btn layui-btn-normal" id="test2_hash">上传图片</button>
                                            <div class="layui-upload-list">
                                                <img class="layui-upload-img" id="demo2_hash" style="width:10%;margin-left:10%">
                                                <p id="demoText_hash"></p>
                                            </div>
                                        </div>
                                    </div>
                                </div> -->
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label">等级</label>
                                <div class="layui-input-block">
                                    <select name="level">
							        	<option value="1" selected="">L1级</option>
							        	<option value="2">L2级</option>
										<option value="3">L3级</option>
										<option value="4">L4级</option>
										<option value="5">L5级</option>
										<option value="6">L6级</option>
										<option value="7">L7级</option>
										<option value="8">L8级</option>
										<option value="9">L9级</option>
										<option value="10">L10级</option>
							        </select>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label">状态</label>

                                <div class="layui-input-block">
                                    <select name="state">
										<option value="0">下架</option>
										<option value="1" selected="">上架</option>
									</select>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="save">保存</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var form;
	var imgUrls = [];
    layui.config({
        base: '/src/js/'
    }).use(['admin', 'jquery', 'mockjs', 'table', 'sidebar', 'form', 'upload', "layedit", 'laydate'], function() {
        var $ = layui.jquery,
            admin = layui.admin,
            layer = layui.layer,
            table = layui.table,
            sidebar = layui.sidebar,
            upload = layui.upload;
        var layedit = layui.layedit;
        var laydate = layui.laydate;
        var isAdd = true;
        var id = getUrlParam("id");

        var longitude;
        var latitude;
        form = layui.form;
        var companyPhoto;
        //var environmentPhoto;
		

        var pois;

        var ac = new BMap.Autocomplete( //建立一个自动完成的对象
            {
                "input": "address_input",
                "onSearchComplete": function(e) {
                    console.log(e);
                    pois = e;
                }

            }
        );

        ac.addEventListener("onconfirm", function(e) { //鼠标点击下拉列表后的事件

            var _value = e.item.value;
            console.log("value:" + JSON.stringify(_value));
            $("#address_input").val(_value.street + _value.business);

            var local = new BMap.LocalSearch(_value.city, { //智能搜索
                onSearchComplete: function() {

                    console.log('local:' + local);
                    var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
                    console.log(local);
                    console.log(local.getResults());
                    console.log('local:' + local);
                    latitude = pp.lat;
                    longitude = pp.lng;
                }
            });
            local.search(_value.street + _value.business);
        });


        laydate.render({
            elem: '#endTime_id' //指定元素
        });

        form.render();

        httpPost_async(GET_CITY_LIST, {
            level: 1
        }, function(results) {
            for (var i = 0; i < results.length; i++) {
                $("#provno_id").append("<option value='" + results[i].code + "'>" + results[i].name + "</option>");
            }
        })

        if (id != undefined && id != "") {
            $("#title-label").text('修改考点');
            $("#at_id").val(id);
            isAdd = false;
            httpPost(GET_EXAMINATION_POINT_INFO, {
                id: id
            }, function(results) {
                if (results.province) {
                    httpPost_async(GET_CITY_LIST, {
                        level: 2,
                        parentId: results.province.code
                    }, function(results) {
                        for (var i = 0; i < results.length; i++) {
                            $("#cityno_id").append("<option value='" + results[i].code + "'>" + results[i].name + "</option>");
                        }
                        form.render();
                    })
                }
                if (results.city) {
                    httpPost_async(GET_CITY_LIST, {
                        level: 3,
                        parentId: results.city.code
                    }, function(results) {
                        for (var i = 0; i < results.length; i++) {
                            $("#areacode_id").append("<option value='" + results[i].code + "'>" + results[i].name + "</option>");
                        }
                        form.render();
                    })
                }

                console.log("results.endTime:" + results.endTime);
                form.val("base_form", {
                    "name": results.companyName,
                    "companyAddress": results.companyAddress.split("&&")[0],
                    "companyAddressDetail": results.companyAddress.split("&&")[1],
                    "provno": results.province ? results.province.code : "",
                    "cityno": results.city ? results.city.code : "",
                    "areacode": results.area ? results.area.code : "",
                    "chargeName": results.chargeName,
                    "chargePost": results.chargePost,
                    "chargePhone": results.chargePhone,
                    "mobile": results.mobile,
                    "examinationName": results.examinationName,
                    "examinationCode": results.examinationCode,
                    "longitude": results.longitude,
                    "latitude": results.latitude,
                    "examinationPost": results.examinationPost,
                    "examinationPhone": results.examinationPhone,
                    "email": results.email,
                    "level": results.level,
                    "state": results.state,
                    "createDate": results.createDate,
                    "updateDate": results.updateDate,
                    "endTime": toDateStr(results.endTime)
                });
                longitude = results.longitude;
                latitude = results.latitude;
				imgUrls = results.companyPhoto.split(",");
                companyPhoto = results.companyPhoto;
                environmentPhoto = results.environmentPhoto;
//                 $('#demo1_hash').attr('src', results.companyPhoto);
// 				$('#demo2_hash').attr('src', results.companyPhoto);
                //$('#demo2_hash').attr('src', results.environmentPhoto);
				for(var i=0;i<imgUrls.length;i++){
					if(i == 0){
						$(".layui-upload-list").append('<img class="layui-upload-img" id="photo_' + i + '" style="width: 10%;margin-left: 10%;" onclick="deletePic(' + i + ')" src="' + imgUrls[i] + '"/>');
					}else{
						$(".layui-upload-list").append('<img class="layui-upload-img" style="width: 10%;margin-left: 2%" onclick="deletePic(' + i + ')" src="' + imgUrls[i] + '"/>');
					}
					
				}

            }, function(message) {});
        } else {
            $("#title-label").text('新增考点');
        }

        form.on('submit(save)', function(data) {
            // layer.msg("保存成功");
            var url;
            if (isAdd) {
                url = ADD_EXAMINATION_POINT;
            } else {
                url = UPDATE_EXAMINATION_POINT;
            }
            var params = data.field;
            params.longitude = longitude;
            params.latitude = latitude;
            params.address = data.field.companyAddress;
			params.companyAddress = data.field.companyAddressDetail;
            params.companyPhoto = imgUrls.join(',');
			
			if(isObjNull(params.companyPhoto)){
				layer.msg("请先上传营业执照");
				return false;
			}
            //params.environmentPhoto = environmentPhoto;


            if (isObjNull(params.provno)) {
                layer.msg("请先设置省份");
                return false;
            }
            if (isObjNull(companyPhoto)) {
                layer.msg("请先设置营业执照");
                return false;
            }
            // if (isObjNull(environmentPhoto)) {
            //     layer.msg("请先设置公司环境图片");
            //     return false;
            // }
            httpPost(url, params, function(results) {
                if (isObjNull(id)) {
                    parent.layer.msg("添加成功");
                    window.history.go(-1);
                    // window.location.href="javascript:history.go(-1)";
                } else {
                    parent.layer.msg("修改成功");
                }

            }, function(message) {
                parent.layer.msg(message);
            });

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        form.on('select(provinceSelect)', function(data) {
            $("#cityno_id option").remove();
            $("#areacode_id option").remove();

            $("#cityno_id").append('<option value="" selected="">请选择</option>');
            $("#areacode_id").append('<option value="" selected="">请选择</option>');
            httpPost_async(GET_CITY_LIST, {
                level: 2,
                parentId: data.value
            }, function(results) {
                for (var i = 0; i < results.length; i++) {
                    $("#cityno_id").append("<option value='" + results[i].code + "'>" + results[i].name + "</option>");
                }

                form.render();
            })

        });

        form.on('select(citySelect)', function(data) {
            $("#areacode_id option").remove();
            $("#areacode_id").append('<option value="" selected="">请选择</option>');
            httpPost_async(GET_CITY_LIST, {
                level: 3,
                parentId: data.value
            }, function(results) {
                for (var i = 0; i < results.length; i++) {
                    $("#areacode_id").append("<option value='" + results[i].code + "'>" + results[i].name + "</option>");
                }

                form.render();
            })

        });

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1_hash',
            url: UPLOAD_IMAGE + "?width=800",
            before: function(obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result) {
                    $('#demo1_hash').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }

                //上传成功
                // companyPhoto = res.data.src;
				imgUrls.push(res.data.src);
				$(".layui-upload-list").append('<img class="layui-upload-img" style="width: 10%;margin-left: 2%" onclick="deletePic(' + (imgUrls.length - 1) + ')" src="' + res.data.src + '"/>');
            },
            error: function() {
                //演示失败状态，并实现重传
                var demoText = $('#demoText_hash');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function() {
                    uploadInst.upload();
                });
            }
        });

        //普通图片上传
        // var uploadInst = upload.render({
        //     elem: '#test2_hash',
        //     url: UPLOAD_IMAGE + "?width=800",
        //     before: function (obj) {
        //         //预读本地文件示例，不支持ie8
        //         obj.preview(function (index, file, result) {
        //             $('#demo2_hash').attr('src', result); //图片链接（base64）
        //         });
        //     },
        //     done: function (res) {
        //         //如果上传失败
        //         if (res.code > 0) {
        //             return layer.msg('上传失败');
        //         }

        //         //上传成功
        //         environmentPhoto = res.data.src;

        //     },
        //     error: function () {
        //         //演示失败状态，并实现重传
        //         var demoText = $('#demoText_hash');
        //         demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
        //         demoText.find('.demo-reload').on('click', function () {
        //             uploadInst.upload();
        //         });
        //     }
        // });
		
		
		
    });
	
	function deletePic(i){
		parent.layer.confirm('确定要删除这个图片吗？', function (index) {
			delete imgUrls[i];
			$("#photo_" + i).remove();
			parent.layer.close(index);
		});
	}
</script>